package com.fengke.cn.yejian11.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.fengke.cn.yejian11.Model.Model;
import com.fengke.cn.yejian11.R;
import com.fengke.cn.yejian11.activity.TeaDetailActivity;
import com.fengke.cn.yejian11.config.Config;
import com.fengke.cn.yejian11.impls.TeaImpls;
import com.fengke.cn.yejian11.javabean.Coll;
import com.fengke.cn.yejian11.javabean.TeaList;
import com.fengke.cn.yejian11.utils.RoundImageView;
import com.squareup.picasso.Picasso;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by Administrator on 2017/9/30 0030.
 */

public class TeaAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<TeaList> list;
    private String uid;

    public TeaAdapter(Context context, List<TeaList> list,String uid)
    {
        this.context=context;
        this.list=list;
        this.uid=uid;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_of_teacher,viewGroup,false);
        TeaViewHolder holder=new TeaViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int i) {
        final  TeaList teaList=list.get(i);
        final  TeaViewHolder holder=(TeaViewHolder)viewHolder;
        final Coll coll=new Coll();
        holder.hint.setText(teaList.getHits());
        holder.name.setText(teaList.getActcate_name());
        Picasso.with(context).load(Config.PIC_URL+teaList.getThumb()).into(holder.pic);
        ((TeaViewHolder) viewHolder).pic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, TeaDetailActivity.class);
                intent.putExtra("tea_id",teaList.getId());
                context.startActivity(intent);
            }
        });
        //判断是否已经被收藏
        Retrofit re= Model.getConn();
        TeaImpls impls=re.create(TeaImpls.class);
        Call<Integer> call=impls.is_t_coll(teaList.getId(),uid,"2","pf2cmg5jk9p24f2mv1kjf55bo3");
        call.enqueue(new Callback<Integer>() {
            @Override
            public void onResponse(Call<Integer> call, Response<Integer> response) {
                //返回结果为1表示没有关注，0为已经关注
                //保存返回的结果
                 coll.setStatue(response.body());
                if(response.body()==1)
                {
                    holder.tea_coll.setImageResource(R.drawable.ic_like_fill_1);
                }
                else{
                    holder.tea_coll.setImageResource(R.drawable.ic_like_fill);
                }
            }

            @Override
            public void onFailure(Call<Integer> call, Throwable t) {
                Toast.makeText(context, "收藏判断失败", Toast.LENGTH_SHORT).show();
            }
        });
        //添加收藏和取消收藏
        //如果statue为1，表示可以添加关注，否则是取消关注
        holder.tea_coll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Retrofit re=Model.getConn();
                TeaImpls impls=re.create(TeaImpls.class);
                if (coll.getStatue() ==1)
                {
                    Call<Integer> call=impls.coll("060pn7sjorpnq5fp53kid5st54",uid,teaList.getId(),"2");
                    call.enqueue(new Callback<Integer>() {
                        @Override
                        public void onResponse(Call<Integer> call, Response<Integer> response) {
                            //收藏成功后将statue赋值为0
                            coll.setStatue(0);
                            if (response.body()!=0)
                            {
                                Toast.makeText(context,"收藏成功",Toast.LENGTH_SHORT).show();
                                holder.tea_coll.setImageResource(R.drawable.ic_like_fill);
                            }
                        }

                        @Override
                        public void onFailure(Call<Integer> call, Throwable t) {
                            Toast.makeText(context,"收藏失败",Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }
    @Override
    public int getItemCount() {
        return list.size();
    }
    class  TeaViewHolder extends RecyclerView.ViewHolder
    {
        RoundImageView pic;
        ImageView tea_coll;
        TextView name;
        TextView hint;
        public TeaViewHolder(View itemView) {
            super(itemView);
            tea_coll=(ImageView)itemView.findViewById(R.id.tea_coll);
            pic=(RoundImageView)itemView.findViewById(R.id.tea_list_pic);
            name=(TextView)itemView.findViewById(R.id.tea_list_name);
            hint=(TextView)itemView.findViewById(R.id.tea_list_hint);
        }
    }
}
